package com.his.admin.controller;

import com.his.admin.service.OrderService;
import com.his.admin.service.PatientService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Order;
import com.his.dao.domain.entity.Patient;
import com.his.dao.domain.entity.User;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/patient")
@AllArgsConstructor
@Api(value = "病人模块")
public class PatientController {
    private PatientService patientService;

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all")
    @ApiOperation(value = "获取病人列表",notes = "获取病人列表")
    public RestResult all(@RequestBody ListDto listDto) {
        return patientService.all(listDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add")
    @ApiOperation(value = "增加病人",notes = "增加病人")
    public RestResult add(@RequestBody Patient patient) {
        patient.setCreateTime(LocalDateTime.now());
        if (patientService.save(patient)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all/id")
    @ApiOperation(value = "获取病人id列表",notes = "获取病人id列表")
    public RestResult allId(@RequestBody Patient patient) {
        return patientService.allId(patient);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/IdToName")
    @ApiOperation(value = "获取病人id通过名字",notes = "获取病人id通过名字")
    public RestResult idToName(@RequestBody Patient patient) {
        return patientService.idToName(patient);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/update")
    @ApiOperation(value = "更新病人",notes = "更新病人")
    public RestResult update(@RequestBody Patient patient) {
        patient.setUpdateTime(LocalDateTime.now());
        if (patientService.updateById(patient)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/delete")
    @ApiOperation(value = "删除病人",notes = "删除病人")
    public RestResult delete(@RequestBody List<Integer> ids) {
        if (patientService.removeByIds(ids)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }
}